﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Collections.Generic;
using fcwms.Common;

/// <summary>
///SessionManage 的摘要说明
/// </summary>
public class SessionManage : Page
{
    public SessionManage()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    #region 重写事件==============
    /// <summary>
    /// 重写Page的Onload事件,用于判断用户是否登录
    /// </summary>
    /// <param name="e"></param>
    protected override void OnLoad(EventArgs e)
    {
        //Session.Timeout = 120;
        //Session["UserID"] = "15";
        //Session["UserName"] = "peixf0721";
        //if (Session["username"] == null || Session["roleid"] == null)
        if (Session["username"] == null || Session["roleid"] == null || Session["userpwd"] == null)
        {
            string dizhi = Page.ResolveUrl("~/admin/login.aspx");
            AlertAndRedirectToTop("您未曾登录或登录超时，请重新登录！", dizhi);
            return;
        }
        base.OnLoad(e);
    }

    public static void AlertAndRedirectToTop(string message, string toURL)
    {
        string js = "<script language=javascript>alert('{0}');top.window.location.replace('{1}')</script>";
        HttpContext.Current.Response.Write(string.Format(js, message, toURL));
    }
    #endregion

    #region 用户==================
    /// <summary>
    /// 判断管理员是否已经登录(解决Session超时问题)
    /// </summary>
    public bool IsAdminLogin()
    {
        //如果Session为Null
        if (Session[DTKeys.SESSION_ADMIN_INFO] != null)
        {
            return true;
        }
        else
        {
            //检查Cookies
            string userid = Utils.GetCookie("username", "fcwms"); //解密用户名
            string userpwd = Utils.GetCookie("userpwd", "fcwms"); //解密用户名
            if (userid != "" && userpwd != "")
            {
                fcwms.BLL.dt_users bll = new fcwms.BLL.dt_users();
                fcwms.Model.dt_users model = bll.GetModel(userid, userpwd);
                if (model != null)
                {
                    Session[DTKeys.SESSION_ADMIN_INFO] = model;
                    return true;
                }
            }
        }
        return false;
    }

    /// <summary>
    /// 取得管理员信息
    /// </summary>
    public fcwms.Model.dt_users GetAdminInfo()
    {
        if (IsAdminLogin())
        {
            fcwms.Model.dt_users model = Session[DTKeys.SESSION_ADMIN_INFO] as fcwms.Model.dt_users;
            if (model != null)
            {
                return model;
            }
        }
        return null;
    }

    /// <summary>
    /// 检查管理员权限
    /// </summary>
    /// <param name="channel_id">频道ID</param>
    /// <param name="action_type">操作类型</param>
    public void ChkAdminLevel(int menu_id, string action_type)
    {
        fcwms.Model.dt_users model = GetAdminInfo();
        fcwms.BLL.dt_role bll = new fcwms.BLL.dt_role();
        bool result = bll.Exists(model.role_id, menu_id, action_type);
        if (!result)
        {
            string msbox = "parent.f_errorTab(\"错误提示\", \"您没有管理该页面的权限，请勿尝试非法进入！\")";
            //ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox.ToString(), true);  //修正BUG
            Response.Write("<script type=\"text/javascript\">" + msbox + "</script>");
            Response.End();
        }
    }

    /// <summary>
    /// 检查管理员权限
    /// </summary>
    /// <param name="channel_name">栏目名称</param>
    /// <param name="action_type">操作类型</param>
    public void ChkAdminLevel(string menu_name, string action_type)
    {
        fcwms.Model.dt_users model = GetAdminInfo();
        fcwms.BLL.dt_role bll = new fcwms.BLL.dt_role();
        bool result = bll.Exists(model.role_id, menu_name, action_type);
        if (!result)
        {
            string msbox = "parent.f_errorTab(\"错误提示\", \"您没有管理该页面的权限，请勿尝试非法进入！\")";
            //ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox.ToString(), true);  //修正BUG
            Response.Write("<script type=\"text/javascript\">" + msbox + "</script>");
            Response.End();
        }
    }

    /// <summary>
    /// 检查管理员权限
    /// </summary>
    /// <param name="channel_name">栏目名称</param>
    /// <param name="action_type">操作类型</param>
    /// <returns>bool</returns>
    public bool IsAdminLevel(string menu_name, string action_type)
    {
        fcwms.Model.dt_users model = GetAdminInfo();
        fcwms.BLL.dt_role bll = new fcwms.BLL.dt_role();
        return bll.Exists(model.role_id, menu_name, action_type);
    } 
    #endregion

    #region 枚举==================

    /// <summary>
    /// 统一管理操作枚举
    /// </summary>
    public enum ActionEnum
    {
        /// <summary>
        /// 所有
        /// </summary>
        All,
        /// <summary>
        /// 查看
        /// </summary>
        View,
        /// <summary>
        /// 添加
        /// </summary>
        Add,
        /// <summary>
        /// 修改
        /// </summary>
        Edit,
        /// <summary>
        /// 删除
        /// </summary>
        Delete,
        /// <summary>
        /// 审核
        /// </summary>
        Audit,
        /// <summary>
        /// 回复
        /// </summary>
        Reply,
        /// <summary>
        /// 取消
        /// </summary>
        Cancel,
        /// <summary>
        /// 作废
        /// </summary>
        Invalid
    }

    /// <summary>
    /// 属性类型枚举
    /// </summary>
    public enum AttributeEnum
    {
        /// <summary>
        /// 输入框
        /// </summary>
        Text,
        /// <summary>
        /// 下拉框
        /// </summary>
        Select,
        /// <summary>
        /// 单选框
        /// </summary>
        Radio,
        /// <summary>
        /// 复选框
        /// </summary>
        CheckBox
    }
    #endregion

    public enum line
    {
        line01,
        line02,
        line03,
        line04,
        line05,
        line06,
        line07,
        line08,
        line09,
        line10,
        line11,
        line12,
        line13,
        line14
    }
}